1. Introduction

Bayesian brain mapping (BBM) is a technique for producing individualized functional brain topographic maps from existing group-level network maps. Group-level network maps are commonly group ICA maps or group average parcellations, but other types of networks maps such as non-negative matrix factorization and PROFUMO can be used. In the case of ICA, BBM is known as template ICA (CITE Mejia et al 2020). BBM is a hierarchical source separation model, with priors on the spatial topography and, optionally, on the functional connectivity. The priors are estimated a-priori, so the model can be fit to individual-level fMRI data. The noise-reduction properties of the population-derived priors result highly accurate and reliable individual network topography maps and the functional connectivity between them. Importantly, the subject-level network maps are matched to the corresponding group networks from the template (i.e. parcellations or group ICA maps). Because BBM is applicable to individual-level analysis, it is computationally convenient and has potential clinical utility.

Once a set of group-level network maps has been chosen, there are two steps to performing BBM. Both are implemented in the BayesBrainMap R package.

TODO: ADD DIAGRAM (show progress)

Here, we perform Step 1 using data from the Human Connectome Project (HCP). Specifically, we train CIFTI-format Bayesian brain mapping priors using a variety of ICA and parcellation-based templates, listed below. The population-derived priors described here are available for use for individual-level Bayesian brain mapping. For analysis of individuals from other populations, it is often desirable to train the prior on a set of training subjects representative of that population. To facilitate this, we also provide and describe the code used to produce the HCP-derived priors, so that this workflow can be easily reproduced in other datasets. Finally, we illustrate the use of

For the choice of group-level network maps, we provide several options:

2. Setup

To reproduce this workflow, first follow the setup process outlined in Appendix A.

3. Choosing Training Subjects

Before estimating the BBM priors, we first select a high-quality, balanced subject sample to ensure reliable, representative priors. Starting from the full HCP sample of 1206, we apply the following filtering steps:

  1. Filter Subjects by Sufficient fMRI Scan Duration

    See Appendix B.1 and script: 1_fd_time_filtering.R

  2. Filter Unrelated Subjects

    See Appendix B.2 and script: 2_unrelated_filtering.R

  3. Balance sex within age groups

    See Appendix B.3 and script: 3_balance_age_sex.R

The resulting subject list (valid_combined_subjects_balanced.rds) is used throughout the rest of the analysis.

4. Step 1: Estimate Priors using estimate_prior()

In this step, we estimate group-level statistical priors using the estimate_prior() function from the BayesBrainMap package.

The helper function estimate_and_export_prior() in the file 5_estimate_prior.R wraps the full procedure, handling subject selection, BOLD file path construction, parcellation selection, and output saving.

4.1 Subject List and Scan Selection

The encoding parameter is set to combined to use the final list of subjects saved in Step 3.3 (valid_combined_subjects_balanced.rds), which includes individuals who passed motion filtering in both LR and RL directions in both sessions, were unrelated, and were sex-balanced within age groups.

For each subject, we use REST1 sessions from both phase-enconding directions:

4.2 Temporal Preprocessing Parameters

To standardize scan duration and improve data quality, we apply both initial volume dropping and temporal truncation using parameters handled directly by the estimate_prior() function from the BayesBrainMap package.

Specifically:

See Appendix C for more details.

4.3 Parcellation Choices

We consider two types of group-level parcellations for estimating priors:

Each of these parcellations was used to estimate priors with and without global signal regression (GSR), resulting in eight total priors saved as .rds files. See Table for a summary of the parcellations and GSR combinations.

# This script estimates and saves functional connectivity priors 
# for both spatial topography and connectivity.
# It supports both GICA-based (15/25/50 ICs) and Yeo17 parcellations, 
# with or without global signal regression (GSR).
# For priors using the "combined" subject list, it loads REST1-LR and REST1-RL
# scans for each subject, 
# drops the first 15 volumes, and truncates each scan to approximately 10 minutes.
# Outputs:
# - Priors `.rds` file saved in `dir_results`

source("5_estimate_prior.R")

4.4 Example Usage

Running estimate_prior() on the full "combined" subject list (~350 subjects) takes approximately 27 hours and uses 135 GB of memory.

For an example of how to run estimate_prior() and all relevant parameters, see Appendix E.

5. Visualization

In this section, we visualize both the parcellation maps and the priors outputs (mean and variance) for each parcellation scheme used in the study: Yeo17, 15 IC, 25 IC, and 50 IC.

We also visualize their corresponding functional connectivity (FC) priors.

5.1 Generate and Save Parcellation Visualizations

5.1.1 Yeo17 parcellation

Script: 8_visualization_Yeo17parcellations.R

This script creates one PNG image per parcel (17 in total), where only the selected parcel is colored and all others are white. The parcellation used is Yeo17, created in Appendix D.

Images are saved in data/parcellations_plots/Yeo17.

TODO: add in appendix visualizations?

5.1.2 GICA Parcellations (15, 25, 50 ICs)

Script: 9_visualization_GICAparcellations.R

This script loops over all independent components for each parcellation dimensionality (nIC = 15, 25, 50) and generates two images per component:

  • A cortical surface map (e.g., GICA_15_IC1.png)

  • A subcortical view (e.g., GICA_15_IC1_sub.png)

The resulting images are saved in the following folders:

  • data/parcellations_plots/15IC/

  • data/parcellations_plots/25IC/

  • data/parcellations_plots/50IC/

Each pair of files corresponds to a specific ICA component and captures its spatial map across brain regions.

TODO: add in appendix visualizations?

5.2 Visualize Prior Components

Script: 6_visualization_prior.R

This script loads each estimated prior file from priors_rds/ and plots both the mean and standard deviation components for all independent components (ICs).

All images are organized into folders by number of ICs and GSR setting, e.g.:

data/priors_plots/combined/15IC/GSR=F/

data/priors_plots/combined/25IC/GSR=T/

data/priors_plots/combined/50IC/GSR=F/

data/priors_plots/combined/Yeo17/GSR=T/

5.3 Visual Summary of Priors

In this section, we present a comparative visual summary of the estimated group-level priors.

For each parcellation type Yeo17, 15 ICs, 25 ICs, and 50 IC, we display:

  • First and Last Parcellation Map

  • First and Last Component Mean

  • First and Last Component Standard Deviation

These summaries are shown in a 2-column grid layout per parcellation to highlight spatial structure and variability.

All images were generated using the scripts:

  • 8_visualization_Yeo17parcellations.R

  • 9_visualization_GICAparcellations.R

  • 6_visualization_prior.R

5.3.1 15 ICs

5.3.2 25 ICs

5.3.3 50 ICs

5.3.4 Yeo17

For the Yeo17 parcellation, we show visualizations of the two main networks (DefaultA and DorsAttnA):

5.4 Visualize Functional Connectivity Priors

Script: 7_visualization_FC.R

This step visualizes the Functional Connectivity (FC) prior for each ICA model using both the Cholesky and Inverse-Wishart parameterizations. For each group-level prior in priors_rds/, we compute and plot:

  • Mean FC matrix (off-diagonal values only)

  • Standard deviation of FC estimates (from the variance matrix)

For each prior, the following outputs are saved in the data/priors_plots/combined/FC folder:

PDF files (2 per prior)

  • [prior_name]_FC_Cholesky.pdf

  • [prior_name]_FC_InverseWishart.pdf

Each PDF includes:

  • FC Prior Mean (Page 1)

  • FC Prior Standard Deviation (Page 2)

PNG imgaes (4 per prior)

  • [prior_name]_FC_Cholesky_mean.png

  • [prior_name]_FC_Cholesky_sd.png

  • [prior_name]_FC_InverseWishart_mean.png

  • [prior_name]_FC_InverseWishart_sd.png

These visualizations allow for a direct comparison of spatial FC structure and uncertainty across priors and estimation methods.

5.4.1 15 ICs

5.4.2 25 ICs

5.4.3 50 ICs

5.4.4 Yeo17

6. Using Priors for Individual-Level Brain Mapping

IN PROGRESS!

In this section, we demonstrate how to apply the population-level priors estimated in Section 5 to perform subject-level analysis using the BayesBrainMap package.

The process involves two steps:

  1. Fitting the Bayesian brain mapping model to subject data using a precomputed prior.

  2. Identifying regions of significant deviation from the prior mean (i.e., areas of engagement).

This example uses:

Note: To run this example, users must have access to appropriate subject-level fMRI data. In this case, we use HCP subject *****. Users can replace the file path with their own data accordingly.

7.1 Load Subject-Level fMRI Data and Prior

# Load population prior (15 ICs, GSR = TRUE)
prior <- readRDS(...)

# Load subject fMRI data (CIFTI format)
# Replace with your own file path
BOLD <- ...

The fMRI input must be a CIFTI, NIFTI, or matrix object compatible with the prior.

7.2 Estimate Subject-Level Networks

Once the data is loaded, we fit the Bayesian brain mapping model to obtain individualized functional networks aligned to the prior components:

bMap <- BrainMap(
  BOLD = BOLD,
  prior = prior,
  ...
  )

7.3 Identify Engagement Maps

eng <- engagements(
  bMap = bMap,
  
  )

7.4 Visualize the Results

Appendix A: Setup

To reproduce this workflow, first clone the repository to your local machine or cluster:

git clone https://github.com/mandymejia/BayesianBrainMapping-Templates.git
cd BayesianBrainMapping-Templates

Next, download the required data/ folder from the following OSF link:

https://osf.io/n3wk5/?view_only=0d95b31090a245eb9ef51fe262be60ef

Once downloaded, unzip the folder and place the data/ directory inside your cloned GitHub repository, so the folder structure looks like this:

BayesianBrainMapping-Templates/
├── data/
│   ├── priors_rds/
│   └── parcellations_plots/
│   └── ...
├── src/
│   ├── 0_setup.R
│   └── 1_fd_time_filtering.R
|   └── ...
├── BayesianBrainMapping-Templates.Rmd
└── ...

This section initializes the environment by loading required packages, setting analysis parameters, and defining directory paths.

Important: Before running the workflow, you must review 0_setup.R and install any necessary packages, ensure you have an installation of Connectome Workbench, and update the following variables to match your local or cluster environment:

github_repo_dir <- getwd()
src_dir <- file.path(github_repo_dir, "src")
source(file.path(src_dir, "0_setup.R"))

Appendix B: Subject Filtering

Appendix B.1: Filter Subjects by Sufficient fMRI Scan Duration

We begin by filtering subjects based on the fMRI scan duration after motion scrubbing For each subject, and for each session (REST1, REST2) and encoding direction (LR, RL), we compute framewise displacement (FD) using the fMRIscrub package. We use a lagged and filtered version of FD (CITE Pham Less is More and Power/Fair refs therein) appropriate for multiband data. FD is calculated from the Movement_Regressors.txt file available in the HCP data for each subject, encoding and session.

A volume is considered valid if it passes an FD threshold, and a subject is retained only if both sessions in both encodings have at least 10 minutes (600 seconds) of valid data.

The final subject list includes only those who passed the filtering criteria in both LR and RL encodings and in both visits (REST1 and REST2). This list is referred to as the combined list and is the one used throughout this project.

# This script filters subjects based on motion using framewise displacement (FD)
# from fMRIscrub.
# For each subject, encoding (LR/RL), and session (REST1/REST2), it computes FD 
# and valid scan
# time after excluding high-motion volumes.
# Subjects with ≥10 minutes of valid data in both sessions are retained.
# Outputs (saved in dir_results):
# - Valid subject lists for LR, RL, and combined encodings (intersection)
# - FD summary per subject/session/encoding

source(file.path(src_dir,"1_fd_time_filtering.R")) 

During this step, an FD summary table is generated with the following columns:

  • subject: HCP subject ID

  • session: REST1 or REST2

  • encoding: LR or RL

  • mean_fd: mean framewise displacement

  • valid_time_sec: total duration of valid data in seconds

Preview of FD Summary Table

# Read FD summary
fd_summary <- read.csv(file.path(dir_data, "fd_summary.csv"))

# Display the first 4 rows
knitr::kable(head(fd_summary, 4), caption = "First rows of FD summary table")
First rows of FD summary table
X subject session encoding mean_fd valid_time_sec
1 100206 REST1 LR 0.1017240 858.24
2 100206 REST2 LR 0.1361220 858.96
3 100206 REST1 RL 0.0698779 864.00
4 100206 REST2 RL 0.0824894 863.28

As shown above, subject 100206 qualifies for further analysis because each of the four sessions (REST1/REST2 × LR/RL) contains at least 600 seconds of valid data.

The script is currently designed to filter based on valid time only, but it can be easily adapted to apply additional constraints such as maximum mean FD thresholds if desired (e.g., mean_fd < 0.2).

Appendix B.2: Filter Unrelated Subjects

Building on the previous step, we use the HCP restricted demographic data to exclude related individuals. This step helps ensure the statistical independence of subjects in the group-level priors estimation.

For the combined list of valid subjects derived in the previous step, we:

  1. Subset the HCP restricted demographics to include only those subjects with at least 10 minutes remaining after scrubbing.

  2. Filter by Family_ID to retain a single individual per family.

Note: This step requires access to the HCP restricted data. If you do not have access, you can skip this step, resulting in some related subjects being included in your training data.

# This script filters subjects to retain only unrelated individuals, using Family ID 
# information from the restricted HCP data.
# For each encoding (LR, RL, combined), it selects one subject per family from the 
# FD-valid lists.
# Outputs (saved in dir_personal due to restricted data):
# - Unrelated subject lists for LR, RL, and combined encodings (intersection)

source(file.path(src_dir,"2_unrelated_filtering.R"))

Appendix B.3: Balance Sex Within Age Groups

In the final step of subject selection, we balance sex across age groups to reduce potential demographic bias in priors estimation.

For the combined list of valid and unrelated subjects, we:

  • Subset the HCP unrestricted demographics to include only those subjects.

  • Split subjects by age group and examine the sex distribution within each group.

  • If both sexes are present but imbalanced, we randomly remove subjects from the overrepresented group to achieve balance.

Note: If you are not applying the unrelated subject filtering step (3.2), you can modify the code to subset based on valid_combined_subjects_FD instead of valid_combined_subjects_unrelated.

The final list of valid subjects is saved in dir_results as:

  • valid_combined_subjects_balanced.csv

  • valid_combined_subjects_balanced.rds (used in the prior estimation step)

# This script balances sex within each age group for subjects who passed FD and unrelated filtering.
# For each encoding (LR, RL, combined), it samples subjects to equalize the number
# of males and females per age group, 
# unless an age group includes only one gender (in which case no balancing is applied).
# Uses age and gender information from the unrestricted HCP data.
# Outputs (saved in dir_personal):
# - Sex-balanced subject lists for LR, RL, and combined encodings (as .csv and .rds)

source(file.path(src_dir,"3_balance_age_sex.R"))

Appendix C: Scrubbing and Temporal Truncation

Given the HCP TR of 0.72 seconds, 10 minutes corresponds to:

T_total <- floor(600 / TR_HCP) # ~833 volumes

To define the volumes to scrub (i.e., exclude beyond 10 minutes), we compute:

T_scrub_start <- T_total + 1
scrub_BOLD1 <- replicate(length(BOLD_paths1), T_scrub_start:nT_HCP, simplify = FALSE)
scrub_BOLD2 <- replicate(length(BOLD_paths2), T_scrub_start:nT_HCP, simplify = FALSE)
scrub <- list(scrub_BOLD1, scrub_BOLD2)

Because drop_first = 15 removes frames before truncation, the final retained time series per scan will be slightly shorter than 10 minutes. Approximately:

(833 - 15) * 0.72 = ~589 seconds (~9.8 minutes)

Appendix D: Prepare Yeo17 Parcellation for Prior Estimation

In this step, we load and preprocess a group-level cortical parcellation to be used as the template to estimate the priors in the next step. Specifically, we use the Yeo 17-network parcellation (Yeo_17) and perform the following operations:

The resulting parcellation is saved as Yeo17_simplified_mwall.rds.

# This script simplifies the Yeo 17-network parcellation by collapsing region 
# labels and masking out the medial wall.
# It creates a cleaned version of the parcellation suitable for downstream analyses.
# Output:
# - Saved as RDS file in dir_data: "Yeo17_simplified_mwall.rds"

source(file.path(src_dir,"4_parcellations.R"))

We can visualize the Yeo17 networks and their corresponding labels:

todo: visualize better in pdf

# Load libraries
library(ciftiTools)
library(rgl)
rgl::setupKnitr()

# Load the parcellation
yeo17 <- readRDS(file.path(dir_data, "Yeo17_simplified_mwall.rds"))
yeo17 <- add_surf(yeo17)

view_xifti_surface(
  xifti = yeo17,
  widget = TRUE,
  title = "Yeo17 Network Parcellation",
  legend_ncol = 6,
  legend_fname = "yeo17_legend.png",
)
# Show legend
knitr::include_graphics("yeo17_legend.png")

Appendix E: Example Function Call for Prior Estimation

# For detailed parameter descriptions, run: ?estimate_prior

estimate_prior(
  BOLD = BOLD_paths1,         # REST1 LR scans (list of file paths)
  BOLD2 = BOLD_paths2,        # REST2 LR scans (same subjects/order as BOLD)
  template = GICA,            # GICA 15-component parcellation (CIFTI dscalar file path)
  GSR = TRUE,                 # Apply global signal regression
  TR = 0.72,                  # Repetition time in seconds
  hpf = 0.01,                 # High-pass filter cutoff in Hz
  Q2 = 0,                     # No nuisance IC denoising
  drop_first = 15,            # Drop first 15 volumes
  scrub = scrub,              # Timepoints to scrub (list format)
  verbose = TRUE              # Print progress updates
)